/*
 * igo: com.dota.framework.schedule.SchedulerManager.java Create On 2011-4-29 下午05:14:41 By Q-Wang
 * $Revision: 1.1 $
 */
package com.ctvit.framework.schedule;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author <a href="mailto:apeidou@gmail.com">Q-Wang</a>
 *
 */
public class SchedulerManager {

	private static SchedulerManager instance = new SchedulerManager();

	private SchedulerFactory schedulerFactory;

	private Scheduler scheduler;

	private SchedulerManager() {
		try {
			StdSchedulerFactory factory = new StdSchedulerFactory();
			//			factory.initialize();
			schedulerFactory = factory;
			scheduler = schedulerFactory.getScheduler();
			scheduler.start();
		} catch (SchedulerException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void start() throws SchedulerException {
		getScheduler().start();
	}

	public static SchedulerFactory getSchedulerFactory() {
		return instance.schedulerFactory;
	}

	public static Scheduler getScheduler() {
		return instance.scheduler;
	}

	public static void scheduleJob(Trigger trigger, JobDetail job) throws SchedulerException {
		getScheduler().scheduleJob(job, trigger);
	}

	public static void updateTrigger(Trigger trigger) throws SchedulerException {
		getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
	}
	public static void removeTrigger(String triggerName, String groupName) throws SchedulerException {
		getScheduler().unscheduleJob(triggerName, groupName);
	}
	public static void removeJob(String jobName, String groupName) throws SchedulerException {
		getScheduler().deleteJob(jobName, groupName);
	}

	public static Trigger getTrigger(String triggerName, String triggerGroup) throws SchedulerException {
		return getScheduler().getTrigger(triggerName, triggerGroup);
	}
	public static void addJob(JobDetail detail) throws SchedulerException {
		getScheduler().addJob(detail, true);
	}
	public static JobDetail getJob(String jobName, String jobGroup) throws SchedulerException {
		return getScheduler().getJobDetail(jobName, jobGroup);
	}

	public static void shutdown() throws SchedulerException {
		getScheduler().shutdown();
	}
}
